package com.wandian.games.mvvm.gameonline;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ledong.lib.minigame.GameCenterHomeFragment;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.eventbus.ChangeMessage;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.games.R;
import com.wandian.games.databinding.GamesFragmentOnLineBinding;
import com.wandian.games.mvvm.gamebook.GameBookFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.jessyan.autosize.utils.ScreenUtils;

/**
 * @author Administrator
 * @date 2019/12/16-13:30
 * @description 小游戏fragment
 **/
@Route(path = RouterConfig.ROUTER_FRAGMENT_GAMES_ON_LINE)
public class GamesOnLineFragment extends BaseMVVMFragment<GamesOnLineViewModel> {

    private GamesFragmentOnLineBinding dataBinding;
    private FragmentManager mSupportFragmentManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.inflate(inflater, R.layout.games_fragment_on_line, container, false);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);
        if(!UIUtils.isEmpty(dataBinding)) EventBus.getDefault().register(this);
        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) dataBinding.viewSys.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.height = ScreenUtils.getStatusBarHeight();;
        dataBinding.viewSys.setLayoutParams(linearParams);
        initViewData();
        initClink();

    }


    private void initClink(){

    }



    private void initViewData(){
        mSupportFragmentManager = getActivity().getSupportFragmentManager();
//        mTransaction = mSupportFragmentManager.beginTransaction();
        GameCenterHomeFragment gamecenter = new GameCenterHomeFragment();
        mSupportFragmentManager.beginTransaction().replace(R.id.fragment_game_on_line, gamecenter).commitAllowingStateLoss();

    }



   @Subscribe(threadMode = ThreadMode.MAIN)
   public void eventBusMessage(ChangeMessage changeMessage) {
       try {
           if(!UIUtils.isEmpty(changeMessage) && !UIUtils.isEmpty(changeMessage.getIsChange())){
               //changeGames
               if(changeMessage.getIsChange().equals("changeGamesOnLine")){
                   Log.d("changeGamesOnLine","changeGamesOnLine" );
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

    @Override
    public void onDestroy() {
        try {
            EventBus.getDefault().unregister(this);
        }catch (Exception e){}
        super.onDestroy();
    }

}
